package com.zf.jsider.service;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;

import com.zf.jsider.entity.Character;

/**
 * @author zhangfeng
 *
 */
public class ExpToExcelService {

	private List<Character> list;

	private HSSFWorkbook wb;

	private HSSFSheet sheet;

	private int rownum = 0;

	public ExpToExcelService(List<Character> list) {
		this.list = list;
		wb = new HSSFWorkbook();
		sheet = wb.createSheet("list");
		expExcel();
	}

	public void expExcel() {
		HSSFRow h = sheet.createRow(rownum++);
		Cell cell = null;
		for (int i = 0; i < columnNames.length; i++) {
			cell = h.createCell(i);
			cell.setCellValue(columnNames[i]);
		}
		Character data = null;
		for (int i = 0; i < list.size(); i++) {
			Character now = list.get(i);
			HSSFRow row = sheet.createRow(rownum++);
			Cell mc;
			int j = 0;
			mc = row.createCell(j++);
			mc.setCellValue(now.getInfo());
			mc = row.createCell(j++);
			mc.setCellValue(now.getSequipment());
			mc = row.createCell(j++);
			mc.setCellValue(now.getEquipment());
			mc = row.createCell(j++);
			mc.setCellValue(now.getGem());
			mc = row.createCell(j++);
			mc.setCellValue(now.getHp());
			mc = row.createCell(j++);
			mc.setCellValue(now.getSice());
			mc = row.createCell(j++);
			mc.setCellValue(now.getIce());
			mc = row.createCell(j++);
			mc.setCellValue(now.getSfire());
			mc = row.createCell(j++);
			mc.setCellValue(now.getFire());
			mc = row.createCell(j++);
			mc.setCellValue(now.getSthunder());
			mc = row.createCell(j++);
			mc.setCellValue(now.getThunder());
			mc = row.createCell(j++);
			mc.setCellValue(now.getSpoison());
			mc = row.createCell(j++);
			mc.setCellValue(now.getPoison());
			mc = row.createCell(j++);
			mc.setCellValue(now.getPrice());
			mc = row.createCell(j++);
			mc.setCellValue(now.getUrl());
		}
	}

	String[] columnNames = new String[] { "身份信息", "评分", "装备评分", "宝石评分", "血上限", "冰", "冰攻击", "火", "火攻击", "玄", "玄攻击", "毒",
			"毒攻击", "价格", "链接" };

	/**
	 * 输出数据流
	 * 
	 * @param os
	 *            输出数据流
	 */
	public ExpToExcelService write(OutputStream os) throws IOException {
		wb.write(os);
		return this;
	}

	/**
	 * 输出到文件
	 * 
	 * @param fileName
	 *            输出文件名
	 */
	public ExpToExcelService writeFile(String name) throws FileNotFoundException, IOException {
		FileOutputStream os = new FileOutputStream("./" + name);
		this.write(os);
		return this;
	}
}
